package com.project.service.impl;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;

/**
 * 业务层父类
 */

public class BaseService {
    private  static SqlSessionFactory factory;

    static {
        try {
            //相对于class文件 得到指定文件的读取流
            Reader r= Resources.getResourceAsReader("mybatis.cfg.xml");
            factory=new SqlSessionFactoryBuilder().build(r);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建会话 一个会话相当于一个连接
     * @return
     */
    public SqlSession getSession(){
        return  factory.openSession();
    }
}
